package com.smasher.modbus.maichong;

import com.smasher.modbus.business.ModbusRtuMaster;
import com.smasher.modbus.proxy.ModbusComm;
import com.smasher.serialport.SerialPortProxy;

public class ModbusMaiChong implements ModbusComm<ModbusSerialPortParam> {

    private ModbusRtuMaster master;

    public ModbusMaiChong() {
        SerialPortProxy.getInstance().setReal(new SerialPortMaiChong());
        if (master == null) {
            master = new ModbusRtuMaster();
        }
    }

    @Override
    public void init(ModbusSerialPortParam param) {
        SerialPortProxy.getInstance().init(param.path, param.baud, param.databits, param.parity, param.stopbits);
    }

    @Override
    public void open() {
        SerialPortProxy.getInstance().open();
    }

    @Override
    public void close() {
        SerialPortProxy.getInstance().close();
    }

    @Override
    public void readCoils(int slave, int startAddress, int numberOfPoints) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.readCoils(slave, startAddress, numberOfPoints);
            SerialPortProxy.getInstance().read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void readCoil(int slave, int address) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.readCoil(slave, address);
            SerialPortProxy.getInstance().read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void readDiscreteInputs(int slave, int startAddress, int numberOfPoints) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.readDiscreteInputs(slave, startAddress, numberOfPoints);
            SerialPortProxy.getInstance().read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void readDiscreteInput(int slave, int address) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.readDiscreteInput(slave, address);
            SerialPortProxy.getInstance().read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void readHoldingRegisters(int slave, int startAddress, int numberOfPoints) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.readHoldingRegisters(slave, startAddress, numberOfPoints);
            SerialPortProxy.getInstance().read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void readHoldingRegister(int slave, int address) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.readHoldingRegister(slave, address);
            SerialPortProxy.getInstance().read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void readInputRegisters(int slave, int startAddress, int numberOfPoints) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.readInputRegisters(slave, startAddress, numberOfPoints);
            SerialPortProxy.getInstance().read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void readInputRegister(int slave, int address) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.readInputRegister(slave, address);
            SerialPortProxy.getInstance().read(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void writeSingleCoil(int slave, int address, boolean value) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.writeSingleCoil(slave, address, value);
            SerialPortProxy.getInstance().write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeSingleRegister(int slave, int address, int value) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            byte[] data = master.writeSingleRegister(slave, address, value);
            SerialPortProxy.getInstance().write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void writeCoils(int slave, int address, boolean[] input) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            int length = input.length;
            int[] target = new int[length];
            for (int i = 0; i < length; i++) {
                target[i] = input[i] ? 1 : 0;
            }

            byte[] data = master.writeCoils(slave, address, length, target);
            SerialPortProxy.getInstance().write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeHoldingRegisters(int slave, int address, int sCount, short[] input) {
        if (master == null) {
            master = new ModbusRtuMaster();
        }
        try {
            int length = input.length;
            int[] target = new int[length];
            for (int i = 0; i < length; i++) {
                target[i] = input[i];
            }
            byte[] data = master.writeHoldingRegisters(slave, address, sCount, target);
            SerialPortProxy.getInstance().write(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
